import json
import time
import numpy as np
import pandas as pd
from rediscluster import RedisCluster
import redis


class Redis(object):

    def __init__(self, db=8) -> None:
        super().__init__()
        self.__cluster_nodes = [{"host": "127.0.0.1", "port": "7000"}, {"host": "127.0.0.1", "port": "7001"}]
        self.__cluster = None
        self.__standalone_nodes = {'host': '127.0.0.1', 'port': '7006'}
        self.__standalone = None
        self.db = db
        self.depth_lasttime = 0 # time.time()
        self.trade_lasttime = 0 # int(time.time()*1000), 服务器时间和本地时间不一致

    @property
    def cluster_nodes(self):
        return self.__cluster_nodes

    @cluster_nodes.setter
    def cluster_nodes(self, nodes):
        self.__cluster_nodes = nodes

    @property
    def cluster(self):
        if self.__cluster is None:
            self.__cluster = RedisCluster(startup_nodes=self.cluster_nodes, decode_responses=True, skip_full_coverage_check=True)
        return self.__cluster

    def cluster_operator(self, side: str, key, value=None, start=0, end=-1):
        if side == 'get':
            res = self.cluster.get(key)
        elif side == 'lrange':
            res = self.cluster.lrange(key, start=start, end=end)
        elif side == 'zadd':
            res = self.cluster.zadd(key, value)
        else:
            raise NotImplemented
        return res

    @property
    def standalone_nodes(self):
        return self.__standalone_nodes

    @standalone_nodes.setter
    def standalone_nodes(self, nodes):
        self.__standalone_nodes = nodes
        return self.__standalone_nodes

    @property
    def standalone(self):
        if self.__standalone is None:
            self.__standalone = redis.Redis(**self.standalone_nodes, db=self.db)
        return self.__standalone

    def standalone_operator(self, side: str, key=None, value=None, start=0, end=-1):
        if side == 'keys':
            res = self.standalone.keys()
        elif side == 'get':
            res = self.standalone.get(key)
        elif side == 'set':
            res = self.standalone.set(key, value)
        elif side == 'lrange':
            res = self.standalone.lrange(key, start=start, end=end)
        elif side == 'zadd':
            res = self.cluster.zadd(key, value)
        elif side == 'hset':
            res = self.standalone.hset(name=key, key=value[0], value=value[1])
        elif side == 'hget':
            res = self.standalone.hget(name=key, key=value)
        elif side == 'mget':
            res = self.standalone.mget(key)
        else:
            raise NotImplemented
        return res

    def usage(self):
        print('Usage:')
        print('\t1. set cluster_nodes or standalone_nodes, default localhost:7000')
        print('\t2. call cluster_operator or standlone_operator')
        return False


# if __name__ == '__main__':
#     symbol = 'dot_usdt'
#     rd = Redis(db=0)
#     rd.standalone_nodes = {'host': '127.0.0.1', 'port': '6379'}
#     keys = [f'{symbol}:orders'+str(i) for i in range(1, 8+1)]
#     keys = ['dot_usdt:index_price', 'dot_usdt:balance']
#     values = rd.standalone_operator('mget', keys)
#     print(values)

redisdb = Redis()
